'''
Created on 10 Sep 2009

@author: jack
'''
def XmlPrim(coords):
    xml_coords='<xy>'
    #get all the coordinates and format them properly.
    for i in range(0,len(coords)):
        temp_xml_coords=str(coords[i][0])+','+str(coords[i][1])
        xml_coords+=temp_xml_coords+' '
    #must end with the beginning coordinate
    temp_xml_coords=str(coords[0][0])+','+str(coords[0][1])+'</xy>'
    xml_coords+=temp_xml_coords
    return xml_coords

def XmlWriterList(shape_list, name,num, gaps):
    if num==0:
        f=open(name+'.xml', 'w')
        #opening parts of file
        f.write('<?xml version="1.0" encoding="Shift_JIS" ?><header versionnumber="3"><biginlib lastmod="109-8-4 16:7:42" lastacc="109-8-4 16:7:42"><libname>'+name+' LEDIT 10.20.0</libname><units>0.000000,0.000000</units>')
        f.close()
    f=open(name+'.xml', 'a')
    f.write('<bgnstr creation="108-8-27 16:48:35" lastmod="109-8-2 20:1:42"><strname>gap'+str(gaps[num])+'</strname>')
    for i in range(0,len(shape_list)):
        f.write('<boundary>');
        f.write('<layer number="46">');
        f.write(XmlPrim(shape_list[i].coords));
        f.write('</layer>');  
        f.write('</boundary>'); 
    f.write('</bgnstr>');
    if num==len(gaps)-1:
        f.write('<bgnstr creation="108-8-27 16:48:35" lastmod="109-8-4 16:7:42"><strname>pattern</strname>')
        #closing part of file
        for i in range(0, len(gaps)):
            f.write('<sref><sname>gap'+str(gaps[i])+'</sname><strans reflection="0" absolutemagnification="0" absoluteangle="0" /><xy>-40000,40000</xy></sref>')  
        f.write('</bgnstr></biginlib></header>')   
    f.close()